package com.example;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * ClassName: JDBCConnection
 * Package: com.example
 * Description:
 *
 * @Author: liu
 * @Create: 2025/5/28 9:26
 * @Version:1.0
 */
public class JDBCConnection {
    private static String driver;
    private static String url;
    private static String uname;
    private static String password;

    static {
        try (InputStream in = JDBCConnection.class.getClassLoader().getResourceAsStream("db.properties")) {
            Properties prop = new Properties();
            prop.load(in);
            driver = prop.getProperty("driver");
            url = prop.getProperty("url");
            uname = prop.getProperty("uname");
            password = prop.getProperty("password");
            Class.forName(driver); // 加载MySQL驱动
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("数据库配置加载失败", e);
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, uname, password);
    }
}
